package controller

import (
	"github.com/gin-gonic/gin"
	"go-backend-simple-scaffold/common/resp"
	"go-backend-simple-scaffold/global"
	"go-backend-simple-scaffold/model/entity"
	"go-backend-simple-scaffold/model/request"
	"go-backend-simple-scaffold/service"
	"go-backend-simple-scaffold/utils"
	"go.uber.org/zap"
)

type UserController struct {
}

func (u *UserController) Register(c *gin.Context) {
	var r request.Register
	err := c.ShouldBindJSON(&r)
	if err != nil {
		resp.FailWithMessage(err.Error(), c)
		return
	}
	// 自动验证
	err = utils.Verify(r, utils.RegisterVerify)
	if err != nil {
		resp.FailWithMessage(err.Error(), c)
		return
	}
	user := &entity.GbssUser{
		UserName:    r.UserName,
		PassWord:    r.PassWord,
		NickName:    r.NickName,
		Avatar:      r.Avatar,
		Phone:       r.Phone,
		Email:       r.Email,
		Enable:      r.Enable,
		AuthorityId: r.AuthorityId,
	}
	userReturn, err := service.SvcGroupApp.Register(*user)
	if err != nil {
		global.GBSS_LOG.Error("注册失败！", zap.Error(err))
		resp.FailWithDetailed(gin.H{
			"user": userReturn,
		}, "注册失败:"+err.Error(), c)
		return
	}
	resp.OkWithDetailed(gin.H{
		"user": userReturn,
	}, "注册成功", c)
}
